﻿//准备ExtJS环境
Ext.require
( 
    [
        'Ext.form.field.File',
        'Ext.grid.*',
        'Ext.data.*',
        'Ext.util.*',
        'Ext.state.*',
        'Ext.form.*'
    ]
);

//定义用户表格控件
Ext.define
( 
    'Project.Grid',
    {
        extend: 'Ext.grid.Panel',

        //编辑窗体
        win_edi: null,

        //检索窗体
        win_search: null,

        //附件管理窗体
        win_attachment: null,

        //附件上传窗体
        win_upload: null,

        //初始化组件
        initComponent: function ()
        {
            Ext.apply
            ( 
                this,
                {
                    listeners:
                    {
                        //设置两个指头的滚动
                        render: Common.ScrollableRender
                    },
                    dockedItems:
                    [
                        {
                            xtype: 'toolbar',
                            height: 36,
                            items:
                            [
                                {
                                    iconCls: 'icon-refresh',
                                    text: '刷新',
                                    scope: this,
                                    hidden: false,
                                    tooltip: '刷新数据',
                                    handler: this.onRefreshClick
                                },
                                '-',
                                {
                                    iconCls: 'icon-view',
                                    text: '查看',
                                    itemId: 'button-view',
                                    disabled: true,
                                    hidden: false,
                                    scope: this,
                                    tooltip: '查看工程项目详细信息',
                                    handler: this.onViewClick
                                },
                                {
                                    iconCls: 'icon-search',
                                    text: '检索',
                                    itemId: 'quantity-search',
                                    scope: this,
                                    hidden: false,
                                    tooltip: '检索养护工程量',
                                    handler: this.onSearchClick
                                }
                            ]
                        }
                    ]
                }
            );
            this.callParent();
            this.getSelectionModel().on( 'selectionchange', this.onSelectionChange, this );
        },

        //处理选中项目变化事件
        onSelectionChange: function ( selectionModel, selections )
        {
            //判断是否选中项目
            if ( selections.length === 0 )
            {
                //禁用查看按钮
                this.down( '#button-view' ).setDisabled( true );
            }
            else
            {
                //启用查看按钮
                this.down( '#button-view' ).setDisabled( false );
            }
        },

        //处理刷新按钮点击事件
        onRefreshClick: function ()
        {
            //刷新数据
            this.store.load();
        },

        //处理查看提交按钮点击事件
        onViewClick: function ()
        {
            //获得选中的行
            var selection = this.getView().getSelectionModel().getSelection()[0];

            //判断是否选中行
            if ( selection )
            {
                //查看工程详细信息
                window.open( Common.MakeAbsoluteUrl( 'ViewProject?' ) + 'projectID=' + selection.data.ProjectID );
            }
        },

        //处理上传按钮点击事件
        onUploadClick: function ()
        {
            //获得数据存储对象
            var store = this.store;

            //获得选中的行
            var selection = this.getView().getSelectionModel().getSelection()[0];

            //判断是否选中行
            if ( selection )
            {
                //上传附件
                this.onManageAttachment( selection.data.ProjectID );
            }
        },

        //处理检索按钮点击事件
        onSearchClick: function ()
        {
            //获得数据存储对象
            var store = this.store;

            //判定窗体是否已经创建
            if ( !this.win_search )
            {
                //创建窗体
                this.win_search = Ext.create
                ( 
                    'Ext.window.Window',
                    {
                        title: '检索项目',
                        maximizable: false,
                        maximized: false,
                        width: 480,
                        height: 540,
                        autoScroll: false,
                        bodyBorder: false,
                        draggable: true,
                        isTopContainer: true,
                        modal: true,
                        resizable: false,
                        bodyPadding: 0,
                        closeAction: 'hide',
                        closable: false,
                        layout: 'fit',
                        items:
                        [
                            {
                                xtype: 'form',
                                frame: false,
                                bodyPadding: 12,
                                bodyStyle: 'background-color:White;margin:0;',
                                bodyBorder: false,
                                border: 0,
                                defaultType: 'textfield',
                                fieldDefaults:
                                {
                                    labelAlign: 'left',
                                    labelWidth: 72,
                                    anchor: '100%'
                                },
                                items:
                                [
                                    {
                                        xtype: 'combobox',
                                        itemId: 'cboOrganization',
                                        fieldLabel: '所属单位',
                                        store: store_organization,
                                        displayField: 'Name',
                                        valueField: 'ID',
                                        queryMode: 'local',
                                        hideTrigger: false,
                                        forceSelection: false,
                                        typeAhead: true,
                                        autocomplete: true,
                                        allowBlank: true
                                    },
                                    {
                                        xtype: 'textfield',
                                        itemId: 'txtProjectCode',
                                        fieldLabel: '项目编号',
                                        allowBlank: true
                                    },
                                    {
                                        xtype: 'textfield',
                                        itemId: 'txtProjectName',
                                        fieldLabel: '项目名称',
                                        emptyText: '80个字符',
                                        allowBlank: true,
                                        maxLength: 80
                                    },
                                    {
                                        xtype: 'combobox',
                                        itemId: 'cboCategory',
                                        fieldLabel: '项目类别',
                                        emptyText: '项目类别',
                                        store: store_master_category,
                                        displayField: 'Name',
                                        valueField: 'ID',
                                        queryMode: 'local',
                                        hideTrigger: false,
                                        forceSelection: false,
                                        typeAhead: true,
                                        autocomplete: true,
                                        allowBlank: true
                                    },
                                    {
                                        xtype: 'combobox',
                                        itemId: 'cboRoadway',
                                        fieldLabel: '线路名称',
                                        emptyText: '项目线路名称',
                                        store: store_master_roadway,
                                        displayField: 'PinYin',
                                        valueField: 'Name',
                                        queryMode: 'local',
                                        hideTrigger: false,
                                        forceSelection: false,
                                        typeAhead: true,
                                        autocomplete: true,
                                        allowBlank: true,
                                        enableKeyEvents: true
                                    },
                                    {
                                        xtype: 'panel',
                                        layout: 'column',
                                        border: 0,
                                        items:
                                        [
                                            {
                                                columnWidth: 1 / 2,
                                                xtype: 'datefield',
                                                itemId: 'txtStartDate_L',
                                                fieldLabel: '开工日期',
                                                format: 'Y/m/d',
                                                allowBlank: true,
                                                vtype: 'dateRange',
                                                endDateField: 'txtStartDate_H'
                                            },
                                            {
                                                xtype: 'displayfield',
                                                value: '　　 ~　　 ',
                                                columnWidth: 1 / 6
                                            },
                                            {
                                                columnWidth: 1 / 3,
                                                xtype: 'datefield',
                                                itemId: 'txtStartDate_H',
                                                format: 'Y/m/d',
                                                allowBlank: true,
                                                vtype: 'dateRange',
                                                beginDateField: 'txtStartDate_L'
                                            }
                                        ]
                                    }
                                ],
                                buttons:
                                [
                                    {
                                        text: '确定',
                                        height: 26,
                                        margin: 3,
                                        handler: function ()
                                        {
                                            //获得表单对象
                                            var form = this.up( 'form' );

                                            //检查数据是否合法
                                            if ( form.getForm().isValid() )
                                            {
                                                //隐藏窗体
                                                this.up( 'window' ).hide();

                                                //清除过滤条件
                                                store.filters.clear();

                                                //过滤数据
                                                store.filter
                                                ( 
                                                    [
                                                        {
                                                            property: 'OrganizationID',
                                                            value: form.down( '#cboOrganization' ).getValue()
                                                        },
                                                        {
                                                            property: 'ProjectCode',
                                                            value: form.down( '#txtProjectCode' ).getValue()
                                                        },
                                                        {
                                                            property: 'ProjectName',
                                                            value: form.down( '#txtProjectName' ).getValue()
                                                        },
                                                        {
                                                            property: 'CategoryID',
                                                            value: form.down( '#cboCategory' ).getValue()
                                                        },
                                                        {
                                                            property: 'Roadway',
                                                            value: form.down( '#cboRoadway' ).getValue()
                                                        },
                                                        {
                                                            property: 'GrantNumber',
                                                            value: form.down( '#txtGrantNumber' ).getValue()
                                                        },
                                                        {
                                                            property: 'StartDate_L',
                                                            value: form.down( '#txtStartDate_L' ).getValue()
                                                        },
                                                        {
                                                            property: 'StartDate_H',
                                                            value: form.down( '#txtStartDate_H' ).getValue()
                                                        }
                                                    ]
                                                );
                                            }
                                        }
                                    },
                                    {
                                        text: '取消',
                                        height: 26,
                                        margin: 3,
                                        handler: function ()
                                        {
                                            //隐藏窗体
                                            this.up( 'window' ).hide();
                                        }
                                    }
                                ]
                            }
                            ]
                    }
                );
            }

            //显示窗体
            this.win_search.show();
        }
    }
);

//挂接环境准备完毕事件
Ext.onReady
( 
    function ()
    {
        //初始化快速提示
        Ext.QuickTips.init();

        //设置状态保存提供者（当前设置为Cookie）
        Ext.state.Manager.setProvider( Ext.create( 'Ext.state.CookieProvider', { path: _Current_ActionUrl } ) );

        //初始化附加验证类型
        Common.InitAdditionalValidation();

        //创建线路存储对象
        store_master_roadway = Ext.create
        ( 
            'Ext.data.Store',
            {
                fields: ['Name', 'PinYin'],
                data: master_roadway
            }
        );

        //创建分类
        store_master_category = Ext.create
        ( 
            'Ext.data.Store',
            {
                fields: ['ID', 'Name'],
                data: master_category
            }
        );

        //创建附件数据存储对象
        store_attachment = Ext.create
        ( 
            'Ext.data.Store',
            {
                root: 'data',
                autoLoad: false,
                remoteFilter: true,
                remoteSort: true,
                model: 'ProjectAttachmentModel',
                proxy:
                {
                    type: 'ajax',
                    batchActions: false,
                    reader:
                    {
                        type: 'json',
                        root: 'data'
                    },
                    listeners:
                    {
                        exception: function ( proxy, response, operation, options )
                        {
                            //处理错误
                            Common.HandleProxyError( proxy, response, operation, options );
                        }
                    }
                }
            }
        );

        //创建数据存储
        store_organization = Ext.create
        ( 
            'Ext.data.Store',
            {
                model: 'Organization',
                autoLoad: true,
                proxy:
                {
                    type: 'ajax',
                    url: Common.MakeControllerUrl( 'Organization', 'Reference?includeEmpty=false' ),
                    reader:
                    {
                        type: 'json',
                        root: 'data'
                    }
                }
            }
        );

        //创建数据存储对象
        var store = Ext.create
        ( 
            'Ext.data.Store',
            {
                pageSize: 20,
                autoSync: true,
                autoLoad: true,
                root: 'data',
                remoteFilter: true,
                remoteSort: true,
                model: 'ProjectModal',
                proxy:
                {
                    type: 'ajax',
                    batchActions: false,
                    url: Common.MakeAbsoluteUrl( 'ClosedSelect' ),
                    reader:
                    {
                        type: 'json',
                        root: 'data'
                    },
                    listeners:
                    {
                        exception: function ( proxy, response, operation, options )
                        {
                            //处理错误
                            Common.HandleProxyError( proxy, response, operation, options );
                        }
                    }
                }
            }
        );

        Ext.create
        ( 
            'Project.Grid',
            {
                store: store,
                stateful: true,
                stateId: 'project-closed-state-grid',
                height: 600,
                width: '100%',
                id: 'project-grid',
                title: '',
                renderTo: 'data-grid',
                columnLines: true,
                viewConfig:
                {
                    stripeRows: true,
                    loadingText: '数据加载中...',
                    singleSelect: true
                },
                //创建分页工具栏
                bbar: Ext.create
                ( 
                    'Ext.PagingToolbar',
                    {
                        store: store,
                        displayInfo: true,
                        displayMsg: '第 {0} - 第 {1} 条记录，共 {2} 条记录',
                        emptyMsg: ''
                    }
                ),
                columns:
                [
                    {
                        text: '所属单位',
                        width: 160,
                        sortable: true,
                        align: 'left',
                        dataIndex: 'Organization',
                        hideable: false
                    },
                    {
                        text: '项目编号',
                        width: 160,
                        sortable: true,
                        align: 'left',
                        dataIndex: 'ProjectCode',
                        hideable: false
                    },
                    {
                        text: '项目名称',
                        width: 160,
                        minWidth: 160,
                        flex: 1,
                        sortable: true,
                        align: 'left',
                        dataIndex: 'ProjectName',
                        hideable: false
                    },
                    {
                        text: '项目类别',
                        width: 120,
                        sortable: true,
                        align: 'left',
                        dataIndex: 'Category'
                    },
                    {
                        text: '线路名称',
                        width: 120,
                        sortable: true,
                        align: 'left',
                        dataIndex: 'Roadway'
                    },
                    {
                        text: '起点桩号',
                        width: 80,
                        sortable: true,
                        align: 'left',
                        dataIndex: 'BeginStake'
                    },
                    {
                        text: '终止桩号',
                        width: 80,
                        sortable: true,
                        align: 'left',
                        dataIndex: 'EndStake'
                    },
                    {
                        text: '项目批号',
                        width: 80,
                        sortable: true,
                        align: 'left',
                        dataIndex: 'GrantNumber'
                    },
                    {
                        text: '开工日期',
                        width: 80,
                        sortable: false,
                        align: 'right',
                        renderer: Ext.util.Format.dateRenderer( 'Y/m/d' ),
                        dataIndex: 'StartDate'
                    },
                    {
                        text: '竣工日期',
                        width: 80,
                        sortable: false,
                        align: 'right',
                        renderer: Ext.util.Format.dateRenderer( 'Y/m/d' ),
                        dataIndex: 'CompletionDate'
                    },
                    {
                        text: '项目负责',
                        width: 80,
                        sortable: true,
                        align: 'left',
                        dataIndex: 'ProjectLeader'
                    },
                    {
                        text: '技术负责',
                        width: 80,
                        sortable: true,
                        align: 'left',
                        dataIndex: 'TechnicalChief'
                    },
                    {
                        text: '质检负责',
                        width: 80,
                        sortable: true,
                        align: 'left',
                        dataIndex: 'QualityChief'
                    },
                    {
                        text: '建安预算（万）',
                        width: 100,
                        sortable: true,
                        align: 'right',
                        renderer: Ext.util.Format.numberRenderer( '0,000.00' ),
                        dataIndex: 'Budget'
                    },
                    {
                        text: '建安批复（万）',
                        width: 100,
                        sortable: true,
                        align: 'right',
                        renderer: Ext.util.Format.numberRenderer( '0,000.00' ),
                        dataIndex: 'ApprovedBudget'
                    },
                    {
                        text: '创建日期',
                        width: 80,
                        sortable: true,
                        align: 'right',
                        renderer: Ext.util.Format.dateRenderer( 'Y/m/d' ),
                        dataIndex: 'CreateDate'
                    },
                    {
                        text: '关闭日期',
                        width: 80,
                        sortable: true,
                        align: 'right',
                        renderer: Ext.util.Format.dateRenderer( 'Y/m/d' ),
                        dataIndex: 'CloseDate'
                    }
                ]
            }
        );

        $( window ).bind
        ( 
            "resize",
            function ()
            {
                //调整表格宽度
                Common.AdjustGridWidth( 'project-grid', 'body_content' );
            }
        );

        //调整表格宽度
        Common.AdjustGridWidth( 'project-grid', 'body_content' );
    }
);
